package com.cz.nongyetongv5.utils;

import android.content.Context;
import android.util.Log;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.bean.BeiJianDanWei;
import com.cz.nongyetongv5.bean.ChouYanJiGou;
import com.cz.nongyetongv5.bean.DuChaXunCha;
import com.cz.nongyetongv5.bean.JianChaDanWei;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.LinkMan;
import com.cz.nongyetongv5.bean.Town;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.utils.HttpUtils.OnRequestExecuted;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class UpdateDataUtil {

	protected DatabaseHelper db = null;

	protected Context context = null;

	protected String authUserName = null;

	protected String authPassword = null;

	protected User user;

	protected ConfigUtil configUtil = null;

	protected String modify_time = null;

	public UpdateDataUtil(DatabaseHelper db, Context context, User user) {
		this.db = db;
		this.context = context;

		authUserName = new ConfigUtil(context.getResources()).getRestUsername();

		authPassword = new ConfigUtil(context.getResources()).getRestPassword();

		this.user = user;

		configUtil = new ConfigUtil(context.getResources());

		if (user == null) {

			this.user = new User();

			this.user.setUsername("zyz");

			this.user.setTown_path_id(configUtil.getRegionCode());

		}

	}

	// �ݹ��ȡJSON
	private void readJson(JSONObject jsonObject, List<Object> towns) {

		// ��ȡ���е�key
		Iterator<String> keys = jsonObject.keys();

		while (keys.hasNext()) {
			// �������key
			String key = keys.next();

			// ���ݶ�ȡ
			try {

				if (CharsetUtil.isNotNull(key)) {
					// ��ȡ����
					JSONObject childObject = jsonObject.getJSONObject(key);

					String code = childObject.getString("code");

					String parent_code = null;

					if (childObject.has("parent_code")
							&& CharsetUtil.isNotNull(childObject
							.getString("parent_code"))) {
						parent_code = childObject.getString("parent_code");
					} else if (childObject.has("parent")
							&& CharsetUtil.isNotNull(childObject
							.getString("parent"))) {

						parent_code = childObject.getString("parent");
					}

					String name = childObject.getString("name");

					Town town = new Town(code, parent_code, name, modify_time);

					if (childObject.isNull("cell")
							&& town.getCode().toString().length() <= 6) {

					} else {
						towns.add(town);
					}

					if (!childObject.isNull("cell")) {
						JSONObject cell = childObject.getJSONObject("cell");
						readJson(cell, towns);
					}

				}

			} catch (JSONException e) {

				System.out.println(e.getMessage());
			}
		}

	}

	/**
	 * ��������
	 */
	public void downloadTown(String json) throws Exception {

		List<Object> towns = new ArrayList<Object>();

		JSONObject jsonObject = new JSONObject(json);

		modify_time = jsonObject.getJSONObject("modified")
				.getString("datetime");

		String maxTime = db.queryMaxMotifyTime("TOWN", "MODIFY_TIME",
				user.getKnowRegion());

		if (maxTime.compareTo(modify_time) < 1) {

			db.delete("TOWN");

			readJson(jsonObject.getJSONObject("result"), towns);

			db.insertTown(towns);

		}

	}

	public void add(String tableName) {

		if ("TOWN".equals(tableName)) {

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getResources().getString(
					R.string.download_towninfo);

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					try {
						downloadTown(responseBody);
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("BEIJIANDANWEI".equals(tableName)) {

			String maxTime = db.queryMaxMotifyTime(tableName, "ADDTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_beijiandanweiInfo)
					+ "add/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<BeiJianDanWei> beiJianDanWeis = new ArrayList<BeiJianDanWei>();

								String addTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject bjdwJsonObj = jsonArray
											.getJSONObject(i);

									if (!bjdwJsonObj.isNull("name")) {

										String id = bjdwJsonObj.getString("id");

										String name = bjdwJsonObj
												.getString("name");

										String code = bjdwJsonObj
												.getLong("region_code") + "";

										Integer kind = bjdwJsonObj
												.getInt("kind");

										Double longitude = bjdwJsonObj
												.isNull("longitude") ? null
												: bjdwJsonObj
												.getDouble("longitude");

										Double latitude = bjdwJsonObj
												.isNull("latitude") ? null
												: bjdwJsonObj
												.getDouble("latitude");

										String category = null;

										if (!bjdwJsonObj.isNull("category")) {
											category = bjdwJsonObj
													.getString("category");
										}

										String tel = bjdwJsonObj
												.getString("tel");

										String mobile = null;

										if (!bjdwJsonObj.isNull("mobile")) {
											mobile = bjdwJsonObj
													.getString("mobile");
										}

										String address = bjdwJsonObj
												.getString("address");

										String charge = bjdwJsonObj
												.getString("charge");

										BeiJianDanWei beiJianDanWei = new BeiJianDanWei(
												id, name, code, addTime, null,
												null, address, kind, longitude,
												latitude, category, charge,
												tel, mobile);

										beiJianDanWeis.add(beiJianDanWei);
									}

								}

								db.insertOrUpdateBeiJianDanWei(beiJianDanWeis);
							}

						} catch (JSONException e) {

							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("JIANCHADANWEI".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "ADDTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jianchadanweiInfo)
					+ "add/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<JianChaDanWei> jianChaDanWeis = new ArrayList<JianChaDanWei>();

								String addTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject bjdwJsonObj = jsonArray
											.getJSONObject(i);

									if (!bjdwJsonObj.isNull("id")) {

										String id = bjdwJsonObj.getString("id");

										String name = bjdwJsonObj
												.getString("name");

										String code = bjdwJsonObj
												.getLong("region_code") + "";

										JianChaDanWei jianChaDanWei = new JianChaDanWei(
												id, name, code, addTime, null,
												null);

										jianChaDanWeis.add(jianChaDanWei);
									}

								}

								db.insertOrUpdateJianChaDanWei(jianChaDanWeis);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);
		}

		if ("JIANGUANRENYUAN".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "ADDTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jiancharenyuan)
					+ "add/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<JianChaRenYuan> jianChaDanWeis = new ArrayList<JianChaRenYuan>();

								String addTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject jsonObjItem = jsonArray
											.getJSONObject(i);

									if (!jsonObjItem.isNull("id")) {

										String id = jsonObjItem.getString("id");

										String name = "δ֪";

										if (!jsonObjItem.isNull("realname")) {
											name = jsonObjItem
													.getString("realname");
										}

										Integer rank = jsonObjItem
												.getInt("rank");

										String code = jsonObjItem
												.getLong("region_code") + "";

										JianChaRenYuan jianChaDanWei = new JianChaRenYuan(
												id, name, rank, code, addTime,
												null, null);

										jianChaDanWeis.add(jianChaDanWei);
									}

								}

								db.insertOrUpdateJianChaRenYuan(jianChaDanWeis);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("LINKMAN".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "ADDTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_lianxiren)
					+ "add/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			Log.d("synservice", url);

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<LinkMan> linkMans = new ArrayList<LinkMan>();

								String addTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject linkManObjJson = jsonArray
											.getJSONObject(i);

									String id = linkManObjJson.getString("id");

									String realName = linkManObjJson
											.getString("realname");

									Integer sex = linkManObjJson.getInt("sex");

									String tel = linkManObjJson
											.getString("tel");

									String mobile = linkManObjJson
											.getString("mobile");

									String departmentName = linkManObjJson
											.getString("department_name");

									String post = linkManObjJson
											.getString("post");

									Integer rank = linkManObjJson
											.getInt("rank");

									Double longitude = linkManObjJson
											.isNull("longitude") ? null
											: linkManObjJson
											.getDouble("longitude");

									Double latitude = linkManObjJson
											.isNull("latitude") ? null
											: linkManObjJson
											.getDouble("latitude");

									String regionCode = linkManObjJson
											.getLong("region_code") + "";

									LinkMan linkMain = new LinkMan(id,
											realName, sex, tel, mobile,
											departmentName, post, rank,
											longitude, latitude, regionCode,
											addTime, null, null);

									linkMans.add(linkMain);

								}

								db.insertOrUpdateLinkMan(linkMans);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("CHOUYANJIGOU".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "ADDTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jiancejigouInfo)
					+ "add/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<ChouYanJiGou> chouYanJiGous = new ArrayList<ChouYanJiGou>();

								String addTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject jsonObjItem = jsonArray
											.getJSONObject(i);

									String id = jsonObjItem.getString("id");

									String name = jsonObjItem.getString("name");

									String code = jsonObjItem
											.getLong("region_code") + "";

									String tel = null;

									if (!jsonObjItem.isNull("tel")) {
										tel = jsonObjItem.getString("tel");
									}

									ChouYanJiGou chouYanJiGou = new ChouYanJiGou(
											id, name, code, addTime, null,
											null, tel);

									chouYanJiGous.add(chouYanJiGou);

								}

								db.insertOrUpdateChouYangJiGou(chouYanJiGous);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}
		if ("DUCHAXUNCHA".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = "http://192.168.0.66:81/index.php/rest/dict/inspectitems/region_code/3201/username/"
					+ user.getUsername() + "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {
								String modifytime = jsonObj
										.getString("modifytime");
								JSONArray jsonArray = jsonObj
										.getJSONArray("result");
								List<DuChaXunCha> duChaXunCha = new ArrayList<DuChaXunCha>();
								for (int i = 0; i < jsonArray.length(); i++) {
									JSONObject jsonObjItem = jsonArray
											.getJSONObject(i);

									String kind = jsonObjItem.getString("kind");
									String trade = "0";
									if (!jsonObjItem.getString("trade")
											.equalsIgnoreCase("null"))
										trade = jsonObjItem.getString("trade");
									String title = jsonObjItem
											.getString("title");
									String problem = jsonObjItem
											.getString("problem");
									String region_code = jsonObjItem
											.getString("region_code");
									String add_time = jsonObjItem
											.getString("add_time");
									String modify_time = jsonObjItem
											.getString("modify_time");
									DuChaXunCha duchaxuncha = new DuChaXunCha(
											region_code, kind, trade, title,
											problem, add_time, modify_time,
											null);
									duChaXunCha.add(duchaxuncha);
								}

								db.insertOrUpdateDuChaXunCha(duChaXunCha);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}
	}

	/**
	 * ����
	 *
	 * @param tableName
	 */
	public void update(String tableName) {

		if ("BEIJIANDANWEI".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_beijiandanweiInfo)
					+ "modify/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String updateTime = jsonObj.getJSONObject(
									"modified").getString("datetime");

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<BeiJianDanWei> beiJianDanWeis = new ArrayList<BeiJianDanWei>();

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject bjdwJsonObj = jsonArray
											.getJSONObject(i);

									if (!bjdwJsonObj.isNull("name")) {

										String id = bjdwJsonObj.getString("id");

										String name = bjdwJsonObj
												.getString("name");

										String code = bjdwJsonObj
												.getLong("region_code") + "";

										Integer kind = bjdwJsonObj
												.getInt("kind");

										Double longitude = bjdwJsonObj
												.isNull("longitude") ? null
												: bjdwJsonObj
												.getDouble("longitude");

										Double latitude = bjdwJsonObj
												.isNull("latitude") ? null
												: bjdwJsonObj
												.getDouble("latitude");

										String category = null;

										if (!bjdwJsonObj.isNull("category")) {
											category = bjdwJsonObj
													.getString("category");
										}

										String tel = bjdwJsonObj
												.getString("tel");

										String mobile = null;

										if (!bjdwJsonObj.isNull("mobile")) {
											mobile = bjdwJsonObj
													.getString("mobile");
										}

										String address = bjdwJsonObj
												.getString("address");

										String charge = bjdwJsonObj
												.getString("charge");

										BeiJianDanWei beiJianDanWei = new BeiJianDanWei(
												id, name, code, null,
												updateTime, null, address,
												kind, longitude, latitude,
												category, charge, tel, mobile);

										beiJianDanWeis.add(beiJianDanWei);
									}

								}

								db.insertOrUpdateBeiJianDanWei(beiJianDanWeis);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("JIANCHADANWEI".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jianchadanweiInfo)
					+ "modify/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								String modifyTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<JianChaDanWei> jianChaDanWeis = new ArrayList<JianChaDanWei>();

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject bjdwJsonObj = jsonArray
											.getJSONObject(i);

									if (!bjdwJsonObj.isNull("id")) {

										String id = bjdwJsonObj.getString("id");

										String name = bjdwJsonObj
												.getString("name");

										String code = bjdwJsonObj
												.getLong("region_code") + "";

										JianChaDanWei jianChaDanWei = new JianChaDanWei(
												id, name, code, null,
												modifyTime, null);

										jianChaDanWeis.add(jianChaDanWei);
									}

								}

								db.insertOrUpdateJianChaDanWei(jianChaDanWeis);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("JIANGUANRENYUAN".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jiancharenyuan)
					+ "modify/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<JianChaRenYuan> jianChaDanWeis = new ArrayList<JianChaRenYuan>();

								String modifyTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject jsonObjItem = jsonArray
											.getJSONObject(i);

									if (!jsonObjItem.isNull("id")) {

										String id = jsonObjItem.getString("id");

										String name = "δ֪";

										if (!jsonObjItem.isNull("realname")) {
											name = jsonObjItem
													.getString("realname");
										}

										Integer rank = jsonObjItem
												.getInt("rank");

										String code = jsonObjItem
												.getLong("region_code") + "";

										JianChaRenYuan jianChaDanWei = new JianChaRenYuan(
												id, name, rank, code, null,
												modifyTime, null);

										jianChaDanWeis.add(jianChaDanWei);
									}

								}

								db.insertOrUpdateJianChaRenYuan(jianChaDanWeis);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("LINKMAN".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_lianxiren)
					+ "modify/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<LinkMan> linkMans = new ArrayList<LinkMan>();

								String modifyTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject linkManObjJson = jsonArray
											.getJSONObject(i);

									String id = linkManObjJson.getString("id");

									String realName = linkManObjJson
											.getString("realname");

									Integer sex = linkManObjJson.getInt("sex");

									String tel = linkManObjJson
											.getString("tel");

									String mobile = linkManObjJson
											.getString("mobile");

									String departmentName = linkManObjJson
											.getString("department_name");

									String post = linkManObjJson
											.getString("post");

									Integer rank = linkManObjJson
											.getInt("rank");

									Double longitude = linkManObjJson
											.isNull("longitude") ? null
											: linkManObjJson
											.getDouble("longitude");

									Double latitude = linkManObjJson
											.isNull("latitude") ? null
											: linkManObjJson
											.getDouble("latitude");

									String regionCode = linkManObjJson
											.getLong("region_code") + "";

									LinkMan linkMain = new LinkMan(id,
											realName, sex, tel, mobile,
											departmentName, post, rank,
											longitude, latitude, regionCode,
											null, modifyTime, null);

									linkMans.add(linkMain);

								}

								db.insertOrUpdateLinkMan(linkMans);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

		if ("CHOUYANJIGOU".equals(tableName)) {
			String maxTime = db.queryMaxMotifyTime(tableName, "MODIFYTIME",
					user.getKnowRegion());

			maxTime = maxTime.replaceAll(" ", "%20");

			String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
					+ context.getString(R.string.update_jiancejigouInfo)
					+ "modify/region_code/" + user.getKnowRegion() + "/time/"
					+ maxTime + "/limit/99999/username/" + user.getUsername()
					+ "/from/1";

			HttpUtils httpUtils = new HttpUtils();

			httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

				@Override
				public void onRequestExecuted(int responseCode,
											  String responseBody) {

					if (CharsetUtil.isNotNull(responseBody)) {

						try {
							JSONObject jsonObj = new JSONObject(responseBody);

							String status = jsonObj.getString("status");

							if (CommonConstants.serverIsOk(status)) {

								JSONArray jsonArray = jsonObj
										.getJSONArray("result");

								List<ChouYanJiGou> chouYanJiGous = new ArrayList<ChouYanJiGou>();

								String modifyTime = jsonObj.getJSONObject(
										"modified").getString("datetime");

								for (int i = 0; i < jsonArray.length(); i++) {

									JSONObject jsonObjItem = jsonArray
											.getJSONObject(i);

									String id = jsonObjItem.getString("id");

									String name = jsonObjItem.getString("name");

									String code = jsonObjItem
											.getLong("region_code") + "'";

									String tel = null;

									if (!jsonObjItem.isNull("tel")) {
										tel = jsonObjItem.getString("tel");
									}

									ChouYanJiGou chouYanJiGou = new ChouYanJiGou(
											id, name, code, null, modifyTime,
											null, tel);

									chouYanJiGous.add(chouYanJiGou);

								}

								db.insertOrUpdateChouYangJiGou(chouYanJiGous);
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

				}
			});

			httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
					authPassword, user);

		}

	}

	public void delete(final String tableName) {

		String urlTabName = "";

		if ("BEIJIANDANWEI".equals(tableName)) {
			urlTabName = "ent";
		} else if ("JIANCHADANWEI".equals(tableName)) {
			urlTabName = "department";
		} else if ("JIANGUANRENYUAN".equals(tableName)) {
			urlTabName = "team";
		} else if ("LINKMAN".equals(tableName)) {
			urlTabName = "contact";
		} else if ("CHOUYANJIGOU".equals(tableName)) {
			urlTabName = "detector";
		}

		String maxTime = db.queryMaxMotifyTime(tableName, "DELETETIME",
				user.getKnowRegion());

		maxTime = maxTime.replaceAll(" ", "%20");

		String url = new ConfigUtil(context.getResources()).getPreHttpUrl()
				+ "rest/recorder/index/table/" + urlTabName
				+ "/type/delete/time/" + maxTime + "/limit/99999/username/"
				+ user.getUsername() + "/from/1";

		HttpUtils httpUtils = new HttpUtils();

		httpUtils.setOnRequestExecuted(new OnRequestExecuted() {

			@Override
			public void onRequestExecuted(int responseCode, String responseBody) {

				if (CharsetUtil.isNotNull(responseBody)) {

					try {
						JSONObject jsonObj = new JSONObject(responseBody);

						String status = jsonObj.getString("status");

						if (CommonConstants.serverIsOk(status)) {

							String deleteTime = jsonObj.getJSONObject(
									"modified").getString("datetime");

							JSONArray jsonArray = jsonObj
									.getJSONArray("result");

							List<String> ids = new ArrayList<String>();

							for (int i = 0; i < jsonArray.length(); i++) {

								JSONObject bjdwJsonObj = jsonArray
										.getJSONObject(i);

								if (!bjdwJsonObj.isNull("id")) {

									String id = bjdwJsonObj.getString("id");

									ids.add(id);
								}

							}

							Map<String, String> times = new HashMap<String, String>();

							times.put("ADDTIME", db.queryMaxMotifyTime(
									tableName, "ADDTIME", user.getKnowRegion()));

							times.put("MODIFYTIME", db.queryMaxMotifyTime(
									tableName, "MODIFYTIME",
									user.getKnowRegion()));

							times.put("DELETETIME", deleteTime);

							db.deleteRecord(tableName, "ID", ids, times);
						}

					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

			}
		});

		httpUtils.sendRequest(url, null, "GET", "UTF-8", authUserName,
				authPassword, user);

	}
}
